package com.centling.smartSealForPhone.activity

import android.content.Intent
import android.os.Bundle
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.application.SmartSeal
import com.centling.smartSealForPhone.constants.SPConstants
import com.wsdk.service.BluetoothLeService
import com.centling.smartSealForPhone.utils.SPUtil
import com.centling.smartSealForPhone.utils.ShowDialog
import kotlinx.android.synthetic.privateCloud.activity_auditor_setting.*

/**
 * Created by Jessie on 2016/8/26.
 */
class AuditorSettingActivity : TitleBarActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_auditor_setting)
        setTitleBarText("设置")

        tv_auditor_info.setOnClickListener { startActivity(Intent(mContext, AuditorInfoActivity::class.java)) }
        tv_auditor_help.setOnClickListener { startActivity(Intent(mContext, AuditorHelpActivity::class.java)) }
        tv_auditor_reset_psw.setOnClickListener { startActivity(Intent(mContext, ModifySelfPassActivity::class.java)) }
        tv_auditor_alarm_info.setOnClickListener { startActivity(Intent(mContext, AlarmListActivity::class.java)) }
        tv_auditor_about.setOnClickListener { startActivity(Intent(mContext, AuditorAboutActivity::class.java)) }
        tv_auditor_function_show.setOnClickListener { startActivity(Intent(mContext,FeatureDemoActivity::class.java)) }
        tv_auditor_main_exist.setOnClickListener {
            ShowDialog.showSelectDialog(mContext, "退出登录", "你确定要退出登录吗？", ""
            ) { tryToLogout() }
        }
    }

    private fun tryToLogout() {
        val ip = SmartSeal.getInstance().ip
        val highQuality = SPUtil.getBoolean(SPConstants.HQ_UPLOAD)
        if (!SPUtil.getBoolean(SPConstants.REMEMBERUSER, false)) {
            SPUtil.clearAll()
        } else {
            val userName = SPUtil.getString(SPConstants.USER_NAME)
            val passWord = SPUtil.getString(SPConstants.USER_PSW)
            val isRemember = SPUtil.getBoolean(SPConstants.REMEMBERUSER)
            SPUtil.clearAll()
            SPUtil.setString(SPConstants.USER_NAME, userName)
            SPUtil.setString(SPConstants.USER_PSW, passWord)
            SPUtil.setBoolean(SPConstants.REMEMBERUSER, isRemember)
        }
        SmartSeal.getInstance().changeIp(ip)
        SPUtil.setBoolean(SPConstants.HQ_UPLOAD, highQuality)
        BluetoothLeService.disconnect()
        AtyManager.finishAllActivities()
        startActivity(Intent(mContext, LoginActivity::class.java))
        finish()
    }
}
